﻿using DeHeng_Mes.Model.Table;
using Dm.util;
using GXWA_DATA.Dao;
using GXWA_DATA.Model.ConfigModel;
using GXWA_DATA.Model.Singleton;
using GXWA_DATA.Model.Table;
using GXWA_DATA.Utils;
using S7.Net;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GXWA_DATA.ThreadTask
{
    public class InitTask
    {
        #region 成员实例
        DataDao initDao = new DataDao();
        #endregion

        /// <summary>
        /// 工单初始化线程
        /// </summary>
        public void InitOrder()
        {
            OrderStatus? orderStatus = initDao.QueryOrder().FirstOrDefault();

            if (orderStatus == null)
                return;

            OrderSingleton.Add(orderStatus);

            List<TraceProduction> traceProductions = initDao.QueryProduction(orderStatus.OrderId);

            traceProductions.ForEach(ProductionSingleton.Add);
        }

        /// <summary>
        /// PLC配置
        /// </summary>
        /// <returns></returns>
        public void InitPlcConfig()
        {
            List<DeviceConfig> deviceConfigs = initDao.QueryDeviceConfig();

            foreach (var item in deviceConfigs)
            {
                CpuType plcType = CpuType.S71200;

                if (item.Nick.Equals("主线"))
                {
                    plcType = CpuType.S71500;
                }
                
                PlcConfigModelSingleton.Add(new PlcConfigModel()
                {
                    plc = new Plc(plcType, item.Ip, 0, 1),
                    Config = item,
                    Cpu = plcType,
                    IsConnected = false,
                    Rack = 0,
                    Slot = 1,
                });
            }
        }
    }
}
